<template>
  <div class="article-list">
    <div class="search">
      <p class="float-left mr-10 mb-10" style="width: 200px">
        <el-input v-model="searchParams.keyWords" placeholder="输入名称"></el-input>
      </p>
      <p class="float-left mr-10 mb-10">
        <el-button type="primary" @click="searchArticle">搜索</el-button>
        <el-button type="primary" @click="resetArticle">重置</el-button>
      </p>
    </div>
    <div class="my-10">
      <el-button type="primary" v-permission="['article:add']" @click="addArticle" size="mini" v-if="isShow">新增文章</el-button>
      <el-button type="primary" @click="downloadArticle" size="mini">导出</el-button>
    </div>
    <bdTable :tableHeader="tableHeader" :tableData="tableData" class="mb-10" />
    <bd-page
      :pageSize="pages"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></bd-page>
    <NewsArticle ref="newsArticle" @update:visible="searchArticle(true)" />
    <ArticleDetail ref="articleAudit" @update:visible="searchArticle"/>
  </div>
</template>
<script>
import { queryArticle, deleteArticle, isHot, exportWz } from '@/api/articles'
import { getImgUrl } from '@/filters/filters.js'
import { mapGetters } from 'vuex'
export default {
  name: 'ArticleList',
  props: {
    wzType: {
      type: String,
      required: true
    },
    onlyWz: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      searchParams: {
        keyWords: null
      },
      tableHeader: {
        type: 'selection',
        header: [
          {
            prop: 'wzType',
            label: '文章类型',
            filter: (row) => {
              return this.$articleType[row.wzType]
            }
          },
          {
            prop: 'title',
            label: '文章标题',
            minWidth: 180,
            overflowTooltip: true
          },
          {
            prop: 'status',
            type: 'tag',
            label: '文章状态',
            statusType: (row) => {
              return row.status ? (row.status === 1 ? 'success' : 'danger') : 'warning'
            },
            filter: (row) => {
              return row.status ? (row.status === 1 ? '已通过' : '已驳回') : '待审核'
            }
          },
          {
            label: '是否热门',
            prop: 'isHot',
            type: 'switch',
            disabled: (row) => {
              const roles = this.$store.getters && this.$store.getters.roles
              return !roles.includes('article:isHot') || row.status !== 1
            },
            handler: this.changeSwitch
          },
          {
            prop: 'picUrl',
            type: 'img',
            label: '文章封面',
            previewSrcList: (row) => {
              return [getImgUrl(row.picUrl)]
            }
          },
          {
            prop: 'createTime',
            label: '创建日期',
            minWidth: 150
          },
          {
            label: '操作',
            type: 'button',
            minWidth: 220,
            btn: [
              {
                id: 1,
                name: '审核',
                handler: this.handleAudit,
                disabled: (row) => {
                  return row.status !== 0
                },
                permission: ['article:audit']
              },
              {
                id: 2,
                name: '编辑',
                handler: this.handleEdit
              },
              {
                id: 3,
                name: '删除',
                type: 'danger',
                handler: this.handleDelete
              }
            ]
          }
        ]
      },
      // 列表数据
      tableData: [],
      // 分页
      pages: {
        page: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  // 计算属性
  computed: {
    ...mapGetters(['user']),
    isShow () {
      if (this.onlyWz) {
        return !this.tableData.length
      } else {
        return true
      }
    }
  },
  watch: {},
  mounted () {
    this.searchArticle()
  },
  methods: {
    async searchArticle (clearPage = false) {
      if (clearPage) {
        this.pages.page = 1
        this.pages.pageSize = 10
      }
      const params = {
        wzType: this.wzType,
        ...this.pages,
        ...this.searchParams,
        deptId: this.user.departmentId || null
      }
      const { total, list } = await queryArticle(params)
      this.total = total
      this.tableData = list
    },
    // 重置
    resetArticle () {
      this.searchParams.name = ''
      this.searchArticle(true)
    },
    // 新增文章
    addArticle () {
      this.$refs.newsArticle.isShow(this.wzType)
    },
    // 导出文章
    downloadArticle () {
      const title = this.$articleType[this.wzType] + '-文章列表数据.xlsx'
      console.log(title)
      exportWz({ type: this.wzType, reqStr: 1 }).then()
        .catch(res => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel'
          })
          const objectUrl = URL.createObjectURL(blob)
          const a = document.createElement('a')
          a.href = objectUrl
          a.download = title
          // a.click()
          // 下面这个写法兼容火狐
          a.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }))
          window.URL.revokeObjectURL(blob)
        })
    },
    // 修改文章
    handleEdit (e, row) {
      this.$refs.newsArticle.isShow(this.wzType, row, true)
    },
    // 删除文章
    handleDelete (e, row) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteArticle({ id: row.id })
        this.searchArticle()
      }).catch(() => {
      })
    },
    // 审核文章
    handleAudit (e, row) {
      this.$refs.articleAudit.isShow(row)
    },
    // 当前文章是否热门
    changeSwitch (row) {
      this.$confirm(`此操作将改文章${row.isHot ? '开启' : '取消'}热门, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const hot = row.isHot ? 1 : 0
        isHot({ id: row.id, isHot: hot })
        this.searchArticle()
      }).catch(() => {
        row.isHot = !row.isHot
      })
    },
    // 条数
    handleSizeChange (val) {
      this.pages.pageSize = val
      this.searchArticle()
    },
    // 页数
    handleCurrentChange (val) {
      this.pages.page = val
      this.searchArticle()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
